package com.example.demo.service.Impl;

import com.example.demo.service.DateService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service("ds")
public class DateServiceImpl implements DateService {
    public Integer[] getCalendarTable(){
        Calendar ca = Calendar.getInstance();
        ca.set(ca.get(ca.YEAR),ca.get(ca.MONTH),1);
        int startWeekDay;
        if(ca.get(ca.DAY_OF_WEEK)==ca.SUNDAY){
            startWeekDay = 7;
        }else{
            startWeekDay = ca.get(Calendar.DAY_OF_WEEK) - 1;
        }

        int daysOfMonth = ca.getActualMaximum(Calendar.DAY_OF_MONTH);

        List<Integer> table = new ArrayList<>();
        for(int i = 1; i < startWeekDay; i++){
            table.add(0);
        }
        for(int i = 1; i < daysOfMonth; i++){
            table.add(i);
        }
        while(table.size()%7!=0){
            table.add(0);
        }

        return table.toArray(new Integer[table.size()]);
    }

    @Override
    public int getYear() {
        Calendar ca = Calendar.getInstance();
        return ca.get(Calendar.YEAR);
    }

    @Override
    public int getMonth() {
        Calendar ca = Calendar.getInstance();
        return ca.get(Calendar.MONTH) + 1;
    }

    @Override
    public int getDay() {
        Calendar ca = Calendar.getInstance();
        return ca.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public String getDayInWeek() {
        Calendar ca = Calendar.getInstance();
        String[] rs = {"日","一","二","三","四","五","六"};
        int v = ca.get(Calendar.DAY_OF_WEEK);
        if(v == Calendar.SUNDAY){
            return "日";
        }else{
            return rs[v-1];
        }

    }
    @Override
    public Date parseDate(String date, String time){
        int year = Integer.parseInt(date.substring(0,4))-1900;
        int month = Integer.parseInt(date.substring(5,7))-1;
        int day = Integer.parseInt(date.substring(8,10));


        int hour = Integer.parseInt(time.substring(0,2));
        int min = Integer.parseInt(time.substring(3,5));

        Date d3 = new Date(year,month,day,hour,min,0);
        return d3;

    }

    @Override
    public String dateFormat(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    @Override
    public Date sub(Date a, Date b) {
        long during = a.getTime() - b.getTime();
        Date dur = new Date(during);
        return dur;
    }

    @Override
    public String DateDur2Str(Date dur) {
        Date zero = new Date(0);
        long durSec  = (dur.getTime() - zero.getTime()) / 1000;
        long day = durSec / 86400;
        long hour = durSec % 86400 / 3600;
        long min = durSec % 86400 % 3600 / 60;
        long sec = durSec % 86400 % 3600 %60;
        String rs = String.format("%d天%d时%d分%d秒",day,hour,min,sec);
        return rs;
    }
}
